package com.lj.back.manage.scaffold.module.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lj.back.manage.scaffold.common.DTO.PageQuery;
import com.lj.back.manage.scaffold.common.response.R;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.common.valid.group.AddStrategy;
import com.lj.back.manage.scaffold.common.valid.group.UpdateStrategy;
import com.lj.back.manage.scaffold.module.sys.DTO.SysPermDTO;
import com.lj.back.manage.scaffold.module.sys.VO.SysPermVO;
import com.lj.back.manage.scaffold.module.sys.annotation.SysLog;
import com.lj.back.manage.scaffold.module.sys.enums.OperateTypeEnum;
import com.lj.back.manage.scaffold.module.sys.service.SysPermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashSet;
import java.util.stream.Collectors;

/**
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/sys/perm")
@Api(tags = "后台管理---权限模块")
@ApiSupport(author = "lv")
@AllArgsConstructor
public class SysPermController {

    private final SysPermService sysPermService;

    @GetMapping("/list")
    @ApiOperation(value = "分页获取权限列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:perm:list")
    public R listPerms(@Validated PageQuery pageQuery){
        IPage<SysPermDTO> sysPermDTOPage =  sysPermService.listPerms(pageQuery);
        IPage<SysPermVO> sysPermVOPage =  CommonUtil.pageBeanConvert(sysPermDTOPage, SysPermVO.class);
        return R.success(sysPermVOPage);
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取全部权限列表")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:menu:list")
    public R listPerms(){
        LinkedHashSet<SysPermDTO> sysPermDTOS =  sysPermService.listPerms();
        if (sysPermDTOS == null || sysPermDTOS.isEmpty()){
            return R.success();
        }
        LinkedHashSet<SysPermVO> sysPermVOS = sysPermDTOS.stream()
                .map(sysPermDTO -> BeanUtil.copyProperties(sysPermDTO,SysPermVO.class))
                .collect(Collectors.toCollection(LinkedHashSet::new));
        return R.success(sysPermVOS);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据ID获取权限详情")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:perm:select")
    @ApiImplicitParam(name = "id", value = "权限ID", example = "1", required = true, dataTypeClass = Long.class)
    public R getPermInfo(@PathVariable Long id){
        SysPermDTO sysPermDTO = sysPermService.getPermInfo(id);
        SysPermVO sysPermVO = BeanUtil.copyProperties(sysPermDTO,SysPermVO.class);
        return R.success(sysPermVO);
    }

    @PostMapping("/add")
    @SysLog(value = "新增权限", operateType = OperateTypeEnum.INSERT)
    @ApiOperation(value = "新增权限")
    @ApiOperationSupport(order = 4,ignoreParameters={"id"})
    @RequiresPermissions("sys:perm:insert")
    public R addMenu(@RequestBody @Validated(AddStrategy.class) SysPermDTO sysPermDTO){
        Long permId = sysPermService.addPerm(sysPermDTO);
        return R.build("id",permId);
    }

    @PatchMapping("/update")
    @SysLog(value = "更新权限", operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "更新权限")
    @ApiOperationSupport(order = 5,ignoreParameters={"name"})
    @RequiresPermissions("sys:menu:update")
    public R updateRole(@RequestBody @Validated(UpdateStrategy.class) SysPermDTO sysPermDTO){
        boolean isSuccess = sysPermService.updatePerm(sysPermDTO);
        return R.build(isSuccess);
    }
}
